package com.noshufou.android.su.service;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import com.noshufou.android.su.provider.PermissionsProvider.Apps;
import com.noshufou.android.su.util.Util;

public class PermissionsDbService extends IntentService
{
  private static final String ATTEMPT_PREF = "clean_permissions_attempt";
  private static final int COLUMN_ALLOW = 6;
  private static final int COLUMN_EXEC_CMD = 5;
  private static final int COLUMN_EXEC_UID = 4;
  private static final int COLUMN_ID = 0;
  private static final int COLUMN_NAME = 3;
  private static final int COLUMN_PACKAGE = 2;
  private static final int COLUMN_UID = 1;
  private static final String[] PROJECTION;
  private static final String TAG = "Su.PermissionsDbService";

  static
  {
    String[] arrayOfString = new String[8];
    arrayOfString[0] = "_id";
    arrayOfString[1] = "uid";
    arrayOfString[2] = "package";
    arrayOfString[3] = "name";
    arrayOfString[4] = "exec_uid";
    arrayOfString[5] = "exec_cmd";
    arrayOfString[6] = "allow";
    arrayOfString[7] = "dirty";
    PROJECTION = arrayOfString;
  }

  public PermissionsDbService()
  {
    super("Su.PermissionsDbService");
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  protected void onHandleIntent(Intent paramIntent)
  {
    Log.d("Su.PermissionsDbService", "onHandleIntent()");
    SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    int i = localSharedPreferences.getInt("clean_permissions_attempt", 0);
    int j = 0;
    if (i > 10)
    {
      Log.d("Su.PermissionsDbService", "tried to many times, wipe permissions.sqlite and start again");
      deleteDatabase("permissions.sqlite");
      j = 1;
    }
    while (true)
    {
      SQLiteDatabase localSQLiteDatabase = null;
      ContentResolver localContentResolver = getContentResolver();
      try
      {
        localSQLiteDatabase = new PermissionsDbOpenHelper(this).getWritableDatabase();
        Log.d("Su.PermissionsDbService", "permissions.sqlite opened");
        String str = null;
        String[] arrayOfString1 = (String[])null;
        if (j == 0)
        {
          str = "dirty=?";
          arrayOfString1 = new String[1];
          arrayOfString1[0] = "1";
        }
        localCursor = localContentResolver.query(PermissionsProvider.Apps.CONTENT_URI, PROJECTION, str, arrayOfString1, null);
        Log.d("Su.PermissionsDbService", "got cursor from su.db");
        while (true)
        {
          boolean bool = localCursor.moveToNext();
          if (!bool)
          {
            if (localSQLiteDatabase != null)
            {
              Log.d("Su.PermissionsDbService", "closing permissions.sqlite");
              localSQLiteDatabase.close();
            }
            PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("permissions_dirty", false).putInt("clean_permissions_attempt", 0).commit();
            while (true)
            {
              return;
              if (i <= 12)
                break;
            }
          }
          Log.d("Su.PermissionsDbService", "row " + localCursor.getLong(0) + " dirty, handle it");
          arrayOfString2 = new String[3];
          arrayOfString2[0] = localCursor.getString(1);
          arrayOfString2[1] = localCursor.getString(4);
          arrayOfString2[2] = localCursor.getString(5);
          if (localCursor.getInt(6) != -2)
            break label443;
          Log.d("Su.PermissionsDbService", "needs deleted");
          localSQLiteDatabase.delete("apps", "uid=? AND exec_uid=? AND exec_cmd=?", arrayOfString2);
          localContentResolver.delete(Uri.withAppendedPath(PermissionsProvider.Apps.CONTENT_URI, "clean"), "uid=? AND exec_uid=? AND exec_cmd=?", arrayOfString2);
          Log.d("Su.PermissionsDbService", "delete completed");
        }
      }
      catch (SQLiteException localSQLiteException)
      {
        while (true)
        {
          Cursor localCursor;
          String[] arrayOfString2;
          Log.e("Su.PermissionsDbService", "encountered an exception, schedule a restart", localSQLiteException);
          localSharedPreferences.edit().putInt("clean_permissions_attempt", i + 1).commit();
          ((AlarmManager)getSystemService("alarm")).set(3, 60000L + SystemClock.elapsedRealtime(), PendingIntent.getBroadcast(this, 0, new Intent("com.noshufou.android.su.UPDATE_PERMISSIONS"), 0));
          if (localSQLiteDatabase == null)
            continue;
          Log.d("Su.PermissionsDbService", "closing permissions.sqlite");
          localSQLiteDatabase.close();
          continue;
          label443: if (localCursor.getInt(6) == -1)
            continue;
          Log.d("Su.PermissionsDbService", "Updating permissions.sqlite for " + localCursor.getString(3));
          localSQLiteDatabase.delete("apps", "uid=? AND exec_uid=? AND exec_cmd=?", arrayOfString2);
          ContentValues localContentValues1 = new ContentValues();
          localContentValues1.put("_id", Long.valueOf(localCursor.getLong(0)));
          localContentValues1.put("uid", Integer.valueOf(localCursor.getInt(1)));
          localContentValues1.put("package", localCursor.getString(2));
          localContentValues1.put("name", localCursor.getString(3));
          localContentValues1.put("exec_uid", Integer.valueOf(localCursor.getInt(4)));
          localContentValues1.put("exec_cmd", localCursor.getString(5));
          localContentValues1.put("allow", Integer.valueOf(localCursor.getInt(6)));
          localSQLiteDatabase.insertOrThrow("apps", null, localContentValues1);
          Log.d("Su.PermissionsDbService", "completed update");
          ContentValues localContentValues2 = new ContentValues();
          localContentValues2.put("dirty", "0");
          localContentResolver.update(ContentUris.withAppendedId(PermissionsProvider.Apps.CONTENT_URI, localCursor.getLong(0)), localContentValues2, null, null);
          Log.d("Su.PermissionsDbService", "updated su.db as clean");
        }
      }
      finally
      {
        if (localSQLiteDatabase != null)
        {
          Log.d("Su.PermissionsDbService", "closing permissions.sqlite");
          localSQLiteDatabase.close();
        }
      }
    }
    throw localObject;
  }

  private class PermissionsDbOpenHelper extends SQLiteOpenHelper
  {
    private static final String DATABASE_NAME = "permissions.sqlite";
    private static final int DATABASE_VERSION = 8;
    private static final String LOG_BLOCK = "CREATE TRIGGER IF NOT EXISTS log_block AFTER INSERT ON logs BEGIN DELETE FROM logs; END;";

    public PermissionsDbOpenHelper(Context arg2)
    {
      super("permissions.sqlite", null, 8);
    }

    private void makePrefs(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS prefs (_id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value TEXT);");
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("key", "notifications");
      localContentValues.put("value", "1");
      paramSQLiteDatabase.insert("prefs", null, localContentValues);
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS apps (_id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER, package TEXT, name TEXT,  exec_uid INTEGER, exec_cmd TEXT, allow INTEGER, notifications INTEGER, logging INTEGER, dirty INTEGER, UNIQUE (uid,exec_uid,exec_cmd));");
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS logs (_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id INTEGER, date INTEGER, type INTEGER);");
      paramSQLiteDatabase.execSQL("CREATE TRIGGER IF NOT EXISTS log_block AFTER INSERT ON logs BEGIN DELETE FROM logs; END;");
      makePrefs(paramSQLiteDatabase);
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      int i = paramInt1;
      if (i < 5)
      {
        paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS apps;");
        paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS logs;");
        onCreate(paramSQLiteDatabase);
      }
      while (true)
      {
        return;
        if (i == 5);
        try
        {
          paramSQLiteDatabase.execSQL("ALTER TABLE apps ADD COLUMN notifications INTEGER");
          paramSQLiteDatabase.execSQL("ALTER TABLE apps ADD COLUMN logging INTEGER");
          i = 6;
          if (i == 6)
          {
            String[] arrayOfString1 = new String[2];
            arrayOfString1[0] = "_id";
            arrayOfString1[1] = "uid";
            localCursor = paramSQLiteDatabase.query("apps", arrayOfString1, null, null, null, null, null);
            if (!localCursor.moveToNext())
            {
              localCursor.close();
              i = 7;
            }
          }
          else
          {
            if (i != 7)
              continue;
            paramSQLiteDatabase.execSQL("CREATE TRIGGER IF NOT EXISTS log_block AFTER INSERT ON logs BEGIN DELETE FROM logs; END;");
            makePrefs(paramSQLiteDatabase);
          }
        }
        catch (SQLiteException localSQLiteException)
        {
          while (true)
          {
            Cursor localCursor;
            Log.e("Su.PermissionsDbService", "notifications and logging columns already exist... wut?", localSQLiteException);
            continue;
            int j = localCursor.getInt(localCursor.getColumnIndex("uid"));
            long l = localCursor.getLong(localCursor.getColumnIndex("_id"));
            ContentValues localContentValues = new ContentValues();
            localContentValues.put("name", Util.getAppName(PermissionsDbService.this, j, false));
            localContentValues.put("package", Util.getAppPackage(PermissionsDbService.this, j));
            String[] arrayOfString2 = new String[1];
            arrayOfString2[0] = String.valueOf(l);
            paramSQLiteDatabase.update("apps", localContentValues, "_id=?", arrayOfString2);
          }
        }
      }
    }
  }
}

/* Location:           D:\Development\Decompile_Workspace\슈퍼유저-41\classes_dex2jar.jar
 * Qualified Name:     com.noshufou.android.su.service.PermissionsDbService
 * JD-Core Version:    0.6.0
 */